package com.example.javabasic.mianshi.fuzhulei;

/**
 * 创建二叉树
 */
public class CreateTreeNode {

    public static void main(String[] args) {

    }


    //创建示例二叉树
    public static TreeNode newTree(){
        int[] arr = {8,6,10,5,7,9,11};
        TreeNode TreeNode = new TreeNode(8);
        for (int i = 1; i < arr.length; i++) {
            TreeNode = setTreeNode(arr[i], TreeNode);
        }
        return TreeNode;
    }
    //插入二叉树, 二叉查找树
    public static TreeNode setTreeNode(Integer val,TreeNode TreeNode){
        TreeNode now = TreeNode;
        while(now!=null){
            if(now.val>val){
                if(now.left ==null){
                    now.left = new TreeNode(val);
                    break;
                }
                now = now.left;
            }else{
                if(now.right ==null){
                    now.right = new TreeNode(val);
                    break;
                }
                now = now.right;
            }
        }
        return TreeNode;
    }

}
